package com.base.commonlib.base.presenter;

import android.app.Activity;
import android.text.TextUtils;


import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.view.ViewI;
import com.base.commonlib.bean.DaoSession;
import com.base.commonlib.bean.Partner;
import com.base.commonlib.bean.PartnerDao;
import com.base.commonlib.bean.User;
import com.base.commonlib.bean.UserDao;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.utils.ToastUtil;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

import io.reactivex.disposables.CompositeDisposable;

/**
 * Created by weikailiang on 2019/4/10.
 */

public abstract class BasePresenter<H extends ViewI> extends PresenterIml<H>{

    protected CompositeDisposable mCompositeDisposable=new CompositeDisposable();
    public User mUser;
    public Partner mCurrentPartner;
    public List<Partner> mCurrentPartList;
    protected Activity mActivity;
    public BasePresenter(H view, Activity activity) {
        super(view);
        this.mActivity = activity;
    }
    @Override
    public void start() {
        try {
            DaoSession daoSession = BaseApp.getDaoSession();
            UserDao userDao = daoSession.getUserDao();
            List<User> users = userDao.loadAll();
            mUser = users.get(0);
            if (mUser == null){
                ARouter.getInstance().build("/mine/pwdlogin").navigation();
                return;
            }
            if (RealNetWorkUtil.TYPE.equals("0")){//吴中城管
                QueryBuilder<Partner> partnerQueryBuilder = daoSession.getPartnerDao().queryBuilder();
                Partner partner = partnerQueryBuilder.where(PartnerDao.Properties.ParentId.eq(mUser.getUserId())).unique();
                mCurrentPartner = partner;
            }else if (RealNetWorkUtil.TYPE.equals("1")){//昆山城管
                mCurrentPartList = BaseApp.getPartners();
                if (mCurrentPartList!=null&&mCurrentPartList.size()>0){
                    mCurrentPartner = mCurrentPartList.get(0);
                }else {
                    mCurrentPartner = null;
                }
            }else {
                mCurrentPartList = BaseApp.getPartners();
                if (mCurrentPartList!=null&&mCurrentPartList.size()>0){
                    mCurrentPartner = mCurrentPartList.get(0);
                }else {
                    mCurrentPartner = null;
                }
            }
        } catch (Exception e) {
            mCurrentPartner = null;
            if (mUser == null){
                ARouter.getInstance().build("/mine/pwdlogin").navigation(mActivity, new NavCallback() {
                    @Override
                    public void onArrival(Postcard postcard) {
                        mActivity.finish();
                    }
                });
                return;
            }
        }
    }
    @Override
    public void release() {
        mCompositeDisposable.dispose();
    }
}
